package com.nsapi.niceschoolapi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nsapi.niceschoolapi.common.base.PageData;
import com.nsapi.niceschoolapi.common.config.MySysUser;
import com.nsapi.niceschoolapi.common.util.ResponseEntity;
import com.nsapi.niceschoolapi.entity.*;
import com.nsapi.niceschoolapi.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class CourseController {
    @Resource
    private CourseService courseService;

    @Resource
    private StudentService studentService;

    @Resource
    private SchoolManageService schoolManageService;


    private Integer sid; //登录学生编号


    //进入正选页面
    @RequestMapping(value = "/course/sel")
    public String course(Model model){
        StudentDB studentDB=studentService.student(MySysUser.loginName());
        model.addAttribute("sid",studentDB);
        sid=studentDB.getSid();
        return "view/student/SelectCourse";
    }

    //分页查询课目信息
    @RequestMapping(value = "/course/selectCourse")
    @ResponseBody
    public PageData<Coursel> selectCourse(@RequestParam(value = "page",defaultValue ="1")Integer page,
                                          @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                          Coursel coursel){
        PageData<Coursel> pageData=new PageData<>();
        PageHelper.startPage(page,limit);
        List<Coursel> coursels=courseService.selCourses(coursel);
        PageInfo pageInfo=new PageInfo(coursels);
        pageData.setData(pageInfo.getList());
        pageData.setCount(pageInfo.getTotal());
        return pageData;
    }
    //查询课程类型
    @RequestMapping(value = "/course/seltype")
    @ResponseBody
    public PageData<Coursel> seltype(){
        PageData<Coursel> pageData=new PageData<>();
        List<Coursel> coursels=courseService.courseType();
        pageData.setData(coursels);
        return pageData;
    }
    //添加学生选课记录
    @RequestMapping(value ="/course/selcourse")
    @ResponseBody
    public ResponseEntity selcourse(StuCourseDB stuCourseDB){
        ResponseEntity responseEntity=new ResponseEntity();
        stuCourseDB.setCreate_by(MySysUser.id());
        stuCourseDB.setCreate_date(new Date());
        int result=courseService.addStuCourse(stuCourseDB);
        if (result>0){
            responseEntity.setAny("msg","添加成功！");
        }else{
            responseEntity.setAny("msg","添加失败！");
        }
            return responseEntity;
    }
    /*//进入学生选课管理页面
    @RequestMapping(value = "/selCourses")
    public String selCourses(){
        return "selCou";
    }*/
    //分页查询学生选课记录信息
 @RequestMapping(value = "/sel")
    @ResponseBody
    public PageData<Coursel> sel(@RequestParam(value = "page",defaultValue ="1")Integer page,
                                 @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                          Coursel coursel){
        PageData<Coursel> pageData=new PageData<Coursel>();
        PageHelper.startPage(page,limit);
        List<Coursel> coursels=courseService.selCourseList(coursel);
        PageInfo pageInfo=new PageInfo(coursels);
        pageData.setData(pageInfo.getList());
        pageData.setCount(pageInfo.getTotal());
        return pageData;
    }
    //查询所有专业信息列表
    @RequestMapping(value = "/selJson")
    @ResponseBody
    public PageData<SchoolManageVO> jlselmajor() {
        PageData<SchoolManageVO> pageData=new PageData<>();
        List<SchoolManageVO> schoolManageList = schoolManageService.jlSelMajor(null);
        pageData.setData(schoolManageList);
        return pageData;
    }

    //实现学生选课记录添加以及课目添加
    @RequestMapping(value = "/addcou")
    @ResponseBody
    public ResponseEntity addcou(Coursel coursel){
        ResponseEntity entity=new ResponseEntity();
        //添加课目信息
        int result=courseService.addcou(coursel);
        if (result>0){
            StuCourseDB stuCourseDB =new StuCourseDB();
            stuCourseDB.setCid(coursel.getCid());
            stuCourseDB.setSid(sid);
            stuCourseDB.setCreate_by(MySysUser.id());
            stuCourseDB.setCreate_date(new Date());
            //添加学生选课记录信息
            int result2=courseService.addStuCourse(stuCourseDB);
            if (result2>0){
                entity.setAny("msg","添加成功！");
            }
        }else{
            entity.setAny("msg","添加失败！");
        }
        return entity;
    }
    //修改课目记录信息
    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseEntity update(Coursel coursel){
        ResponseEntity responseEntity=new ResponseEntity();
        int result=courseService.update(coursel);
        if (result>0){
            responseEntity.setAny("msg","修改成功！");
        }else{
            responseEntity.setAny("msg","修改失败！");
        }
        return responseEntity;
    }

    //删除选课记录
    @RequestMapping(value = "/delcou")
    @ResponseBody
    public ResponseEntity delcou(Integer scid){
        ResponseEntity responseEntity=new ResponseEntity();
        int result=courseService.delcou(scid);
        if (result>0){
            responseEntity.setAny("msg","删除成功！");
        }else{
            responseEntity.setAny("msg","删除失败！");
        }
        return responseEntity;
    }

    //进入退课页面
    @RequestMapping(value = "/seledCourse")
    public String seledCourse(){
        return "dropCourse";
    }
}
